package com.wmx.newspushmonitor;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.rhino.permission.PermissionUtils;
import com.rhino.ui.utils.LogUtils;
import com.rhino.ui.utils.SharedPreferencesUtils;
import com.rhino.ui.utils.ui.ColorUtils;
import com.wmx.newspushmonitor.base.BaseSimpleTitleHttpActivity;
import com.wmx.newspushmonitor.databinding.ActivitySplashBinding;
import com.wmx.newspushmonitor.http.HttpUrl;
import com.wmx.newspushmonitor.http.request.LoginParams;
import com.wmx.newspushmonitor.util.UiUtils;

import java.util.List;

/**
 * @author rhino
 * @since Create on 2018/10/9.
 **/
public class SplashActivity extends BaseSimpleTitleHttpActivity<ActivitySplashBinding> {

    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            "android.permission.GET_INTENT_SENDER_INTENT"
    };

    @Override
    public void setContent() {
        setContentView(R.layout.activity_splash);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    @Override
    public void initView() {
        mActionBarHelper.setBackgroundColor(ColorUtils.WHITE);
        mActionBarHelper.setTitleVisible(false);
        checkPermission();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        showNextPage();
    }

    @Override
    public void callbackHandler(int event, String url, String data, Object reqData) {
        super.callbackHandler(event, url, data, reqData);
        LogUtils.d("data = " + data);
        if (event == EVENT_FAILED) {
            showToast("请求异常");
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        } else if (event == EVENT_SUCCESS) {
            switch (url) {
                case HttpUrl.URL_LOGIN:
                    if ("0".equals(data)) {
                        showToast("账号或密码错误！");
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                        return;
                    }
                    SharedPreferencesUtils.getInstance().putString("user_account", ((LoginParams)reqData).account);
                    SharedPreferencesUtils.getInstance().putString("user_password", ((LoginParams)reqData).pwd);
                    SharedPreferencesUtils.getInstance().putString("token", data);
                    UiUtils.showMainPage(this);
                    finish();
                    break;
                default:
                    break;
            }
        } else if (event == EVENT_PROGRESS) {
            LogUtils.d("progress = " + data);
        }
    }

    private void showNextPage() {
        String account = SharedPreferencesUtils.getInstance().getString("user_account");
        String password = SharedPreferencesUtils.getInstance().getString("user_password");
        if (checkNetWork() && !TextUtils.isEmpty(account) && !TextUtils.isEmpty(password) && SharedPreferencesUtils.getInstance().getBoolean("auto_login", false)) {
            LoginParams p = LoginParams.normal(account, password);
            doPost(HttpUrl.URL_LOGIN, p, p);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!isFinishing()) {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                    }
                }
            }, 1000);
        }
    }

    private void checkPermission() {
        ActivityCompat.requestPermissions(this, PERMISSIONS, 0);
    }

}
